/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_GTK_TREEVIEW_H
#define GENERIK_GTK_TREEVIEW_H

/** \file treeview.h
This file defines a convenient wrapper for the GtkTreeView widget.
*/

#include "treeviewutils.h"

/*
notes: 
	good info on GtkTreeModel: http://davyd.livejournal.com/252351.html
*/

namespace generik
{

	namespace gtk
	{

		/** An encapsulation of the GtkTreeView widget.
		It is derived from gtkwidget, and exposes methods which ease the usage of GtkTreeView.
		*/
		struct treeview
		: widget
		{
			/** Constructor which creates a new GtkTreeView instance, and sets it up with default parameters
			using set_parameters().
			*/
			treeview()
			: widget(gtk_tree_view_new())
			, _model(0)
			{
				GENERIK_TRACER;
				GENERIK_TRACE_VALUE(get_widget());
				set_parameters(false, false, false, false, false);
			}
		
			GtkTreeModel* _model;
		
			/** This method must be specialised to create the GtkTreeModel instance for use by this specialised GtkTreeView.
			*/
			virtual void create_model() = 0;
		
			/** This method must be specialised to create the actual columns and their renderers for the specialised GtkTreeView.
			*/
			virtual void create() = 0;
		
			/** This method must be specialised to add data to the specialised GtkTreeStore of the specialised GtkTreeView.
			*/
			virtual void fill() = 0;
		
			/** Returns the GtkTreeView instance stored in this class.
			*/
			virtual GtkTreeView* get_treeview()
			{
				return GTK_TREE_VIEW(get_widget());
			}
		
			virtual GtkTreeModel* get_model()
			{
				return _model;
			}
		
			void detach_model()
			{
				GENERIK_TRACER;
				GENERIK_TRACE_VALUE(_model);
				GENERIK_GDK_ENTER;
				if (_model == 0) _model = gtk_tree_view_get_model(get_treeview());
				g_object_ref(_model); // Make sure the model stays with us after the tree view unrefs it
				gtk_tree_view_set_model(get_treeview(), NULL); //Detach model from view
				GENERIK_TRACE("detached the model at ["<<_model<<"]");
			}
			void retach_model()
			{
				GENERIK_TRACER;
				GENERIK_TRACE_VALUE(_model);
				GENERIK_GDK_ENTER;
				if (_model)
				{
					gtk_tree_view_set_model(get_treeview(), _model);
					g_object_unref(_model);
				}
				GENERIK_TRACE("retached the model at ["<<_model<<"]");
			}
		
			/** Sets the parameters of the GtkTreeView (with default options).
			*/
			void set_parameters(bool headers_visible, bool hover_expand, bool hover_select, bool enable_search, bool enable_tree_lines)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				gtk_tree_view_set_headers_visible	(get_treeview(), headers_visible);
				gtk_tree_view_set_hover_expand		(get_treeview(), hover_expand);
				gtk_tree_view_set_hover_selection	(get_treeview(), hover_select);
				gtk_tree_view_set_enable_search		(get_treeview(), enable_search);
				gtk_tree_view_set_enable_tree_lines (get_treeview(), enable_tree_lines);
				gtk_tree_view_set_level_indentation (get_treeview(), 50);
				gtk_tree_view_set_show_expanders	(get_treeview(), false);
			}
		
			/** Replaces the GtkTreeModel of the treeview with the specified one.
			*/
			void set_model(GtkTreeModel* model)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				_model = model;
				GENERIK_TRACE_VALUE(_model);
				GENERIK_TRACE_VALUE(GTK_IS_TREE_STORE(_model));
				gtk_tree_view_set_model(get_treeview(), NULL);  // necessary?
				gtk_tree_view_set_model(get_treeview(), _model);
				//g_object_unref(store); // ???
			}
		
			/** Selects a specified row using a string path description.
			For example "10:4:2" will select the 10th first level node's 4th second level node's 2nd third level node's row,
			if it exists. The default is to select the first top-level node's row.
			\note For more information on specifying the string path see the GTK+ documentation.
			*/
			void select_row(const std::string& pathstr = "0")
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				GtkTreePath* firstline = gtk_tree_path_new_from_string("0");
				GtkTreeSelection* selection = gtk_tree_view_get_selection(get_treeview());
				gtk_tree_selection_select_path(selection, firstline);
				gtk_tree_path_free(firstline);
			}
		
			/** Expands the string path specified row. If all is specified as true, that row is recursively expanded.
			See select_row() for a short description of the string path format.
			*/
			void expand_row(const std::string& pathstr = "0", bool all = false)
			{
				GENERIK_TRACER;
				GENERIK_GDK_ENTER;
				GtkTreePath* path = gtk_tree_path_new_from_string(pathstr.c_str());
				gtk_tree_view_expand_row(get_treeview(), path, all);
				gtk_tree_path_free(path);
			}
		
		};
		
	}
	
}

#endif

